package Stack;

import java.util.Stack;

public class evalRPN {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(int i = 0 ; i < tokens.length ; i++){
            String ch = tokens[i];
            if(!isOperator(ch)){
                stack.push(Integer.valueOf(ch));
            }else{
                int num2 = stack.pop();
                int num1 = stack.pop();
                switch(ch){
                    case "+":
                        stack.push(num1 + num2) ;
                        break;
                    case "-":
                        stack.push(num1 - num2) ;
                        break;
                    case "*":
                        stack.push(num1 * num2) ;
                        break;
                    case "/":
                        stack.push(num1 / num2) ;
                        break;
                }
            }
        }
        return stack.pop();
    }

    public boolean isOperator(String ch){
        if(ch.equals("+") || ch.equals("-") || ch.equals("*") || ch.equals("/") ){
            return true;
        }
        return false;
    }
}


